<?php
class omeanalysts_mdl_ome_salescost extends dbeav_model{

    //var $has_export_cnf = true;

  var $export_name = 'AX销售费用表(按账单日期)';

  public function table_name($real=false){
    $table_name = 'ome_delivery';
    if($real){
      return kernel::database()->prefix.$table_name;
    }else{
      return $table_name;
    }
  }

  public function count($filter=null){

    $where = $this->_filter($filter); 

    $sql = "SELECT count(*) as _count from  sdb_finance_bill fb
    inner join sdb_ome_shop f on fb.channel_id=f.shop_id
    where  ".$where." ";
    $row = $this->db->select($sql);
    return intval($row[0]['_count']);
  }

  public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){


    if(!$filter['time_from'] && !$filter['time_to'])
    {
      return false;
    }else{           
      $where = $this->_filter($filter); 
           // var_dump($where);
      $sql = "SELECT  bill_bn,channel_name,fee_obj,member,trade_time,order_bn,fee_type,fee_item,money,charge_status,charge_time,monthly_status
      from  sdb_finance_bill fb
      inner join sdb_ome_shop f on fb.channel_id=f.shop_id
      where  ".$where." "; 
  //var_dump($sql);
      $rows = $this->db->selectLimit($sql,$limit,$offset);

      return $rows;
    }

  }

  public function searchOptions(){
    $parentOptions = parent::searchOptions();
    $childOptions = array(
      'order_bn'=>app::get('base')->_('订单号'),
      );
    return $Options = array_merge($parentOptions,$childOptions);
  }

  public function _filter($filter,$tableAlias=null,$baseWhere=null){

   $where = array(1);

   if(empty($filter['time_from']))
   {
    $where[]=' 1=2 ';
  }
  if(isset($filter['time_from']) && $filter['time_from']){
    $where[] = ' fb.trade_time >='.strtotime($filter['time_from']);
  }
  if(isset($filter['time_to']) && $filter['time_to']){
    $where[] = 'fb.trade_time <'.(strtotime($filter['time_to'])+86399);
  }

  if(isset($filter['shop_id'])   && $filter['shop_id']){
    $where[] = ' f.shop_id  =\''.addslashes($filter['shop_id']).'\'';
  }
  if(!$filter['shop_id'] && $filter['shop_ids']){
    $where[] = ' f.shop_id IN (\''.implode('\',\'', $filter['shop_ids']).'\')';
  }
  unset($filter['shop_id']);

  if(isset($filter['credential_number']) && $filter['credential_number']){
    $where[] = ' fb.credential_number =\''.addslashes($filter['credential_number']).'\'';
  }
  if(isset($filter['order_bn']) && $filter['order_bn']){
    $where[] = ' fb.order_bn =\''.addslashes($filter['order_bn']).'\'';
  }
  if(isset($filter['fee_type']) && $filter['fee_type']){
    $where[] = ' fb.fee_type =\''.addslashes($filter['fee_type']).'\'';
  }
  if(isset($filter['fee_item']) && $filter['fee_item']){
    $where[] = ' fb.fee_item =\''.addslashes($filter['fee_item']).'\'';
  }

  if(isset($filter['bytype']) && $filter['bytype'])
  {
          //var_dump($filter['conditions']);
    if($filter['conditions']=='其他'){
     $where[] = ' fb.bytype = '.addslashes($filter['bytype']).'';
   }elseif($filter['conditions']=='天猫超市'){
     $where[] = ' fb.bytype = '.addslashes($filter['bytype']).'';
   }
 }
 return implode($where,' AND ');
}

public function get_schema(){
  $schema = array(
    'columns' => array(
     'bill_bn' =>
     array(
      'type' => 'varchar(32)',
      'required' => true,
      'label' => '单据编号',
      'is_title' => true,
      'width' => 125,
      'searchtype' => 'nequal',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     'channel_name' =>
     array(
      'type' => 'varchar(255)',
      'required' => true,
      'label' => '渠道名称',
      'editable' => false,
      'searchtype' => 'has',
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => true,
      'is_title' => true,
      'width' => '120',
      ),          
     'fee_obj' =>
     array(
      'type' => 'varchar(255)',
      'required' => true,
      'label' => '费用对象',
      'editable' => false,
      'searchtype' => 'has',
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => true,
      'is_title' => true,
      'width' => '120',
      ),   
     'member' =>
     array(
      'type' => 'varchar(200)',
      'required' => true,
      'label' => '客户/会员',
      'is_title' => true,
      'default_in_list' => true,
      'width' => 260,
      'searchtype' => 'has',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      ), 
     'trade_time' =>
     array (
      'type' => 'time',
      'required' => true,
      'label' => '账单日期',
      'is_title' => true,
      'default_in_list' => true,
      'width' => 70,
      'searchtype' => 'has',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      ),
     'order_bn' =>          
     array(
      'type' => 'varchar(32)',
      'required' => true,
      'label' => '业务订单号',
      'is_title' => true,
      'width' => 125,
      'searchtype' => 'nequal',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     'fee_type' =>
     array(
      'type' => 'varchar(32)',
      'required' => true,
      'label' => '费用类',
      'is_title' => true,
      'width' => 125,
      'searchtype' => 'nequal',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     'fee_item' =>
     array(
      'type' => 'varchar(32)',
      'required' => true,
      'label' => '费用项',
      'is_title' => true,
      'width' => 125,
      'searchtype' => 'nequal',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     'money' =>
     array(
      'type' => 'money',
      'label' => '费用金额',
      'width' => 70,
      'required' => true,
      'default' => 0,
      'editable' => false,
      ),   
     'charge_status' =>
     array(
      'type' =>
      array(
        '0' => '未记账',
        '1' => '已记账',
        ),
      'default' => '0',
      'required' => true,
      'label' => '记账状态',
      'width' => 70,
      'editable' => false,
      'filtertype' => 'yes',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     'charge_time' =>
     array(
      'type' => 'time',
      'required' => true,
      'label' => '记账日期',
      'is_title' => true,
      'default_in_list' => true,
      'width' => 70,
      'searchtype' => 'has',
      'editable' => false,
      'filtertype' => 'normal',
      'filterdefault' => true,
      'in_list' => true,
      ),
     'monthly_status' =>
     array(
      'type' =>
      array(
        '0' => '未记账',
        '1' => '已记账',
        ),
      'default' => '0',
      'required' => true,
      'label' => '月结状态',
      'width' => 70,
      'editable' => false,
      'filtertype' => 'yes',
      'filterdefault' => true,
      'in_list' => true,
      'default_in_list' => false,
      ),
     ),
'idColumn' => 'name',
'in_list' => array(
  0 => 'bill_bn',
  1 => 'channel_name',
  2 => 'fee_obj',
  3 => 'member',
  4 => 'trade_time',
  5 => 'order_bn',
  6 => 'fee_type',
  7 => 'fee_item',
  8 => 'money',
  9 => 'charge_status',
  10 => 'charge_time',
  11 => 'monthly_status',

  ),
'default_in_list' => array(
 0 => 'bill_bn',
 1 => 'channel_name',
 2 => 'fee_obj',
 3 => 'member',
 4 => 'trade_time',
 5 => 'order_bn',
 6 => 'fee_type',
 7 => 'fee_item',
 8 => 'money',
 9 => 'charge_status',
 10 => 'charge_time',
 11 => 'monthly_status',
 ),
);
return $schema;
}

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
      $type = $logParams['type'];
      $logType = 'none';
      if ($type == 'export') {
        $logType = $this->exportLogType($logParams);
      }
      elseif ($type == 'import') {
        $logType = $this->importLogType($logParams);
      }
      return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
      $params = $logParams['params'];
      $type = 'report';
      if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
        $type .= '_purchaseReport_expressAnalysis';
      }
      $type .= '_export';
      return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
      $params = $logParams['params'];
      $type = 'report';
      if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
        $type .= '_purchaseReport_expressAnalysis';
      }
      $type .= '_import';
      return $type;
    }

    function io_title( $filter, $ioType='csv' ){

      switch( $filter ){
        case 'delivery':
        $this->oSchema['csv'][$filter] = array(
          '*:单据编号' => 'bill_bn',
          '*:渠道名称' => 'channel_name',
          '*:服务对象' => 'fee_obj',
          '*:客户/会员' => 'member',
          '*:账单日期' => 'trade_time',
          '*:业务单号' => 'order_bn',
          '*:费用类' => 'fee_type',
          '*:费用项' => 'fee_item',
          '*:费用金额' => 'money',
          '*:记账状态' => 'charge_status',
          '*:记账日期' => 'charge_time',
          '*:月结状态' => 'monthly_status',

          );
        break;
      }
      $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
      return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
      if($curr_sheet == 1){

        $title = array();
        foreach($this->io_title('delivery') as $k => $v ){
          $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
        }

        $data['content']['main'][0] = '"'.implode('","',$title).'"';
      }

      $where = $this->_filter($filter);
      //$sql = "SELECT fb.bill_bn,fb.channel_name,fb.fee_obj,fb.member,fb.trade_time,fb.order_bn,fb.fee_type,fb.fee_item,fb.money,fb.charge_status,fb.charge_time,fb.monthly_status
      //from  sdb_finance_bill fb
      //inner join sdb_ome_shop f on fb.channel_id=f.shop_id
      $sql = "SELECT  fb.bill_bn,fb.channel_name,fb.fee_obj,fb.member,FROM_UNIXTIME(fb.trade_time, '%Y-%m-%d %H:%i:%S') as trade_time,FROM_UNIXTIME(fb.charge_time, '%Y-%m-%d %H:%i:%S') as charge_time,fb.order_bn,fb.fee_type,fb.fee_item,fb.money,fb.charge_status,fb.monthly_status
      from  sdb_finance_bill fb  
      inner join sdb_ome_shop f on fb.channel_id=f.shop_id
      where  ".$where." "; 
      $omedelivery = $this->db->selectLimit($sql,$end,$start);
      foreach ($omedelivery as $k => $aFilter) {
        $exptmp_data = array();
        foreach ($aFilter as $col => $val) {
          $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
          $exptmp_data[] = $aFilter[$col];
        }

        $data['content']['main'][] = implode(',', $exptmp_data);
      }
      return $data;

    }
  }
